package com.ostrich.cloud.ostrichGoodsService.service.impl;

import ch.qos.logback.core.joran.conditional.ThenOrElseActionBase;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.SalesDto;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsException;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsExceptionCode;
import com.ostrich.cloud.ostrichGoodsService.common.status.OstrichSalesStatus;
import com.ostrich.cloud.ostrichGoodsService.infra.model.Sales;
import com.ostrich.cloud.ostrichGoodsService.infra.mapper.SalesMapper;
import com.ostrich.cloud.ostrichGoodsService.service.SalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.ostrichGoodsService.service.param.CreateSalesParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.SelectSalesParam;
//import io.seata.spring.annotation.GlobalTransactional;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 销售量 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {

    @Resource
    private SalesMapper salesMapper;


    @Override
    @GlobalTransactional
    public boolean createSales(CreateSalesParam createSalesParam) {
        Sales sales = BeanUtil.copyProperties(createSalesParam, Sales.class);
        sales.setTime(new Date());
        sales.setStatus(OstrichSalesStatus.FINISH);
        int insert = salesMapper.insert(sales);
        if(insert<1){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }
        return true;
    }

    @Override
    public boolean updateSales(Long orderNo) {
        QueryWrapper<Sales> wrapper=new QueryWrapper<>();
        wrapper.eq("order_number",orderNo);
        Sales sales = salesMapper.selectOne(wrapper);

        if(sales==null){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.SALES_GET_EXCEPTION);
        }

        sales.setStatus(OstrichSalesStatus.CANCEL);
        int i = salesMapper.updateById(sales);
        if(i<1){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }


        return true;
    }

    @Override
    public PageResult<SalesDto> getAllSalesDto(Integer current,Integer size) {

        QueryWrapper<Sales> wrapper=new QueryWrapper<>();
        IPage<Sales> salesIPage = salesMapper.selectPage(new Page<>(current, size), wrapper);
        PageResult<SalesDto> salesDtoPageResult=null;
        salesDtoPageResult = getSalesDtoPageResult(current, size, salesIPage, salesDtoPageResult);

        return salesDtoPageResult;
    }

    private PageResult<SalesDto> getSalesDtoPageResult(Integer current, Integer size, IPage<Sales> salesIPage, PageResult<SalesDto> salesDtoPageResult) {
        if(salesIPage!=null&&salesIPage.getRecords()!=null&&salesIPage.getRecords().size()!=0){
            salesDtoPageResult=new PageResult<>();
            List<SalesDto> salesDtos = BeanUtil.copyToList(salesIPage.getRecords(), SalesDto.class);
            salesDtoPageResult.setElements(salesDtos);
            salesDtoPageResult.setPage(current);
            salesDtoPageResult.setPageSize(size);
            salesDtoPageResult.setTotalPage(salesIPage.getPages());
            salesDtoPageResult.setTotal(salesIPage.getTotal());

        }
        return salesDtoPageResult;
    }

    @Override
    public PageResult<SalesDto> selectSalesCondition(SelectSalesParam param) {

        QueryWrapper<Sales> wrapper=new QueryWrapper<>();
        wrapper.eq("businessman_id",param.getBusinessmanId())
                .between(param.getStartTime()!=null&&param.getEndTime()!=null,"time",param.getStartTime(),param.getEndTime());
        IPage<Sales> salesIPage = salesMapper.selectPage(new Page<>(param.getCurrent(), param.getSize()), wrapper);
        PageResult<SalesDto> salesDtoPageResult=null;

        salesDtoPageResult = getSalesDtoPageResult(param.getCurrent(), param.getSize(), salesIPage, salesDtoPageResult);

        return salesDtoPageResult;
    }
}
